package com.net.oil.widget;
/*
 * Created by lch on 2018/3/12 0012.
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import androidx.annotation.Nullable;;
import androidx.core.content.ContextCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.net.oil.R;

public class SideBarView extends View {

    private Paint paint;
    private Paint paintSelect;

    private int choose = -1;
    private boolean showBackground;

    private int sideBarTextSize;
    private int sideBarTextColor;
    private int sideBarPressTextColor;
    private int sideBarBackground;
    private int sideBarPressBackground;

    private OnChooseLetterChangedListener listener;

    public static String[] letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z", "#"};

    public SideBarView(Context context) {
        super(context);
        init();
    }

    public SideBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SideBarView);
        sideBarTextSize = typedArray.getDimensionPixelSize(R.styleable.SideBarView_sideBarTextSize, 30);
        sideBarTextColor = typedArray.getColor(R.styleable.SideBarView_sideBarTextColor, ContextCompat.getColor(getContext(), R.color.text_grey));
        sideBarPressTextColor = typedArray.getColor(R.styleable.SideBarView_sideBarPressTextColor, ContextCompat.getColor(getContext(), R.color.black));
        sideBarBackground = typedArray.getColor(R.styleable.SideBarView_sideBarBackground, ContextCompat.getColor(getContext(), R.color.white));
        sideBarPressBackground = typedArray.getColor(R.styleable.SideBarView_sideBarPressBackground, ContextCompat.getColor(getContext(), R.color.white));
        typedArray.recycle();

        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(sideBarTextColor);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setTextSize(sideBarTextSize);

        paintSelect = new Paint();
        paintSelect.setAntiAlias(true);
        paintSelect.setTypeface(Typeface.DEFAULT_BOLD);
        paintSelect.setTextSize(sideBarTextSize);
        paintSelect.setColor(sideBarPressTextColor);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getHeight();
        int width = getWidth();
        int perHeight = height / letters.length;

        if (showBackground) {
            canvas.drawColor(Color.parseColor("#D9D9D9"));
        }

        for (int i = 0; i < letters.length; i++) {
            canvas.drawText(letters[i], width / 2 - paint.measureText(letters[i]) / 2, perHeight * i + perHeight, paint);

            if (choose == i) {
                canvas.drawText(letters[i], width / 2 - paint.measureText(letters[i]) / 2, perHeight * i + perHeight, paintSelect);
            }
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float y = event.getY();
        int oldChoose = choose;
        int c = (int) (y / getHeight() * letters.length);
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                showBackground = true;
                if (oldChoose != c && listener != null) {
                    if (c > -1 && c < letters.length) {
                        listener.onChooseLetter(letters[c]);
                        choose = c;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (oldChoose != c && listener != null) {
                    if (c > -1 && c < letters.length) {
                        listener.onChooseLetter(letters[c]);
                        choose = c;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                showBackground = false;
                choose = -1;
                if (listener != null) {
                    listener.onNoChooseLetter();
                }
                invalidate();
                break;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }



    public void setSideBarLetterSelectListener(OnChooseLetterChangedListener listener) {
        this.listener = listener;
    }

    public interface OnChooseLetterChangedListener {

        void onChooseLetter(String s);

        void onNoChooseLetter();

    }
}
